import 'dart:typed_data';

import 'package:bluetooth_protocols_plugin/src/tools/extension_enum.dart';
import 'package:bluetooth_protocols_plugin/src/tools/utils.dart';

// 车辆数据校验类型
enum MessageCheckType {
  none(0), // 无校验
  parity(1), // 奇偶校验
  crc16(2), // CRC16_MODBUS标准计算
  crc32(4), //CRC32计算
  crc64(8); //CRC64

  // 校验值的长度
  int checkLength() {
    switch (this) {
      case MessageCheckType.parity:
        return 1;
      case MessageCheckType.crc16:
        return 2;
      case MessageCheckType.crc32:
        return 4;
      case MessageCheckType.crc64:
        return 1;
      default:
        return 0;
    }
  }

  // 获取校验值
  Uint8List checkBytes(Uint8List data) {
    switch (this) {
      case MessageCheckType.parity:
        return Utils.calculateEvenParity(data);
      case MessageCheckType.crc16:
        return Uint8List(0);
      case MessageCheckType.crc32:
        return Uint8List(0);
      case MessageCheckType.crc64:
        return Utils.calculateCheckSum(data);
      default:
        return Uint8List(0);
    }
  }

  final int rawValue;
  const MessageCheckType(this.rawValue);
  // 通过 rawValue 获取枚举类型
  static MessageCheckType? getTypeByNum(int rawValue) => MessageCheckType.values
      .firstWhereOrNull((activity) => activity.rawValue == rawValue);
}
